package Member.dao.impl;

import Member.dao.MemberDao;
import Member.entiy.Member;
import dao.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MemberDaoImpl extends BaseDao implements MemberDao {
    @Override
    public List<Member> getList(int page, int pagesize) {
        String sql="select * from Member limit ?,?";
        ResultSet rs=this.executeQuery(sql,(page-1)*pagesize,pagesize);
        List<Member> list=new ArrayList<>();
            try {
                while (rs.next()) {
                    list.add(new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getInt(7)));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                this.closeConnection(null,null,rs);
        }
            return list;
    }
    @Override
    public int addInfor(Member m) {
        String sql="INSERT INTO `BurgerKingDB`.`Member`(`Name`, `PhoneNumber`, `Email`, `RegistrationDate`, `Points`, `Level`) VALUES (?,?,?,now(),1000,?);";
        return this.executeUpdate(sql,m.getName(),m.getPhoneNumber(),m.getEmail(),m.getLevel());
    }

    @Override
    public int getCount() {
        String sql="select count(*) from Member";
        ResultSet rs=this.executeQuery(sql);
        try {
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            closeConnection(null,null,rs);
        }
        return 0;
    }
    @Override
    public int deletInfor(Member s) {
        String sql="delete from Member where MemberID=?;";
        return  this.executeUpdate(sql,s.getMemberID());
    }

    @Override
    public List<Member> getLike(String name) {
        String sql="SELECT * FROM Member WHERE Name LIKE '%" + name+"%';";
        ResultSet rs=this.executeQuery(sql);
        List<Member> list=new ArrayList<>();
        try {
            while (rs.next()) {
                list.add(new Member(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getInt(7)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }
}
